package com.vcredit.creditcat.web.api;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vcredit.creditcat.api.IResourceManger;
import com.vcredit.creditcat.dto.resource.ResourceDto;

/**
 * @author chengchao
 *
 */
@Controller
@RequestMapping("/resource")
public class ResourceController extends BaseCreditCatController {
	@Reference(version = "1.0", group = "ftp")
	private IResourceManger resourceManager;

	@RequestMapping(value = "/{resourceCode}", method = RequestMethod.GET)
	public void getResource(HttpServletRequest request, HttpServletResponse response,
			@PathVariable("resourceCode") String resourceCode) throws Exception {
		ResourceDto resourceDto = resourceManager.download(resourceCode);
		response.setHeader("Content-Type", resourceDto.getResourceType().getMediaType());
		response.setStatus(HttpStatus.SC_OK);
		response.getOutputStream().write(resourceDto.getResourceContent());
	}
}
